//
//  UIButton+Extension.swift
//  Sevengrass
//
//  Created by Kipp Dev on 2017/12/29.
//  Copyright © 2017年 Syrena. All rights reserved.
//

import Foundation

extension UIButton {
    /// - 扩展初始化方法
    convenience init(image: UIImage?, title: String?) {
        self.init()
        self.setTitle(title, for: .normal)
        self.setImage(image, for: .normal)
    }

    // MARK: - 扩展闭包，直接添加点击事件，适用于仅做跳转的按钮
    struct AssociatedClosureClass {
        var eventClosure: (_ button: UIButton) -> Void
    }

    private struct AssociatedKeys {
        static var eventClosureObj: AssociatedClosureClass?
        static var data = "data"
    }

    private var eventClosureObj: AssociatedClosureClass {
        set {
            objc_setAssociatedObject(self, &AssociatedKeys.eventClosureObj, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)

        }
        get {
            return (objc_getAssociatedObject(self, &AssociatedKeys.eventClosureObj) as? AssociatedClosureClass)!
        }
    }

    func addTarget(for controlEvents: UIControlEvents, action: @escaping (_ button: UIButton) -> Void) {
        let eventObj = AssociatedClosureClass(eventClosure: action)
        eventClosureObj = eventObj
        addTarget(self, action: #selector(eventExcuate(_:)), for: controlEvents)

    }

    func addTargetAddVc(for controlEvents: UIControlEvents, vc: UIViewController, action: @escaping (_ button: UIButton) -> Void) {
        let eventObj = AssociatedClosureClass(eventClosure: action)
        eventClosureObj = eventObj
        addTarget(vc, action: #selector(eventExcuate(_:)), for: controlEvents)

    }

    @objc private func eventExcuate(_ sender: UIButton) {
        eventClosureObj.eventClosure(sender)
    }

    // MARK: - 为 button 扩展 runtime 附带值
    var data: Any? {
        set {
            objc_setAssociatedObject(self, &AssociatedKeys.data, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        get {
            return objc_getAssociatedObject(self, &AssociatedKeys.data)
        }
    }

}

//为单击手势 扩展闭包
extension UITapGestureRecognizer {

    struct AssociatedClosureClass {
        var eventClosure: (UITapGestureRecognizer) -> Void
    }

    private struct AssociatedKeys {
        static var eventClosureObj: AssociatedClosureClass?
    }

    private var eventClosureObj: AssociatedClosureClass {
        set {
            objc_setAssociatedObject(self, &AssociatedKeys.eventClosureObj, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        get {
            return (objc_getAssociatedObject(self, &AssociatedKeys.eventClosureObj) as? AssociatedClosureClass)!
        }
    }

    func addTarget(action: @escaping (UIGestureRecognizer) -> Void) {
        let eventObj = AssociatedClosureClass(eventClosure: action)
        eventClosureObj = eventObj
        addTarget(self, action: #selector(eventExcuate(_:)))

    }

    @objc private func eventExcuate(_ sender: UITapGestureRecognizer) {
        eventClosureObj.eventClosure(sender)
    }
}

extension UILongPressGestureRecognizer {
    struct AssociatedClosureClass {
        var eventClosure: (UILongPressGestureRecognizer) -> Void
    }

    private struct AssociatedKeys {
        static var eventClosureObj: AssociatedClosureClass?
    }

    private var eventClosureObj: AssociatedClosureClass {
        set {
            objc_setAssociatedObject(self, &AssociatedKeys.eventClosureObj, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        get {
            return (objc_getAssociatedObject(self, &AssociatedKeys.eventClosureObj) as? AssociatedClosureClass)!
        }
    }

    func addTarget(action: @escaping (UIGestureRecognizer) -> Void) {
        let eventObj = AssociatedClosureClass(eventClosure: action)
        eventClosureObj = eventObj
        addTarget(self, action: #selector(eventExcuate(_:)))

    }

    @objc private func eventExcuate(_ sender: UILongPressGestureRecognizer) {
        eventClosureObj.eventClosure(sender)
    }

}
